#
# @author Wójcik Karol <wojcikk@student.agh.edu.pl>
# @licence http://www.gnu.org/copyleft/gpl.html GNU/GPL
#

class GroupsController < ApplicationController
layout 'standard'
before_filter :login_required #, :authorize

  def authorize
    if @session['user'].role != "admin"
        redirect_back_or_default :controller => "error", :action => "info"
    end
  end

  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @group_pages, @groups = paginate :groups, :per_page => 30
    @users = User.find(:all, :conditions => "role = 'prowadzacy'")
    @groups = @groups.sort{|a,b| a.user.name <=> b.user.name}
  end
  
  def list_by_subjects
    @group_pages, @groups = paginate :groups, :per_page => 30
    @users = User.find(:all, :conditions => "role = 'prowadzacy'")
    @groups = @groups.sort{|a,b| a.lesson.subject.name<=>b.lesson.subject.name}
  end
  
  def list_user
    @group_pages, @groups = paginate :groups, :per_page => 10
    if @session['user'].role != "admin"
        @users = User.find(:all, :conditions => ["id = ?", @session['user'].id])
    else
	      @users = User.find(:all, :conditions => "role = 'prowadzacy'")
    end
    
    @my_groups = Group.find(:all, :conditions => ["user_id =?", @session['user'].id])
    if params[:id]==nil
      params[:id] = @session['user'].id
    end
    @groups = Group.find(:all, :conditions => ["user_id = ?", params[:id]])
    @lessons_users = LessonsUser.find(:all, :conditions => ["user_id = ?", @session['user'].id])
    
    
  end

  def show
    @group = Group.find(params[:id])
    @users = User.find(:all, :conditions => "role = 'prowadzacy'")
  end

  def new
    @group = Group.new
    @users = User.find(:all, :conditions => "role = 'prowadzacy'")
  end

  def new2
    @group = Group.new
    @users = User.find(:all, :conditions => "role = 'prowadzacy'")
  end

  def create
    @group = Group.new(params[:group])
    @users = User.find(:all, :conditions => "role = 'prowadzacy'")
    if @group.save
      flash[:notice] = 'Group was successfully created.'
      if @session['user'].role == 'prowadzacy' 
        redirect_to :action => 'list_user'
      else 
        redirect_to :action => 'list'
      end
      
    else
      render :action => 'new'
    end
  end

  def edit
    @group = Group.find(params[:id])
    @users = User.find(:all, :conditions => "role = 'prowadzacy'")
  end

  def update
    @group = Group.find(params[:id])
    @users = User.find(:all, :conditions => "role = 'prowadzacy'")
    if @group.update_attributes(params[:group])
      flash[:notice] = 'Group was successfully updated.'
      redirect_to :action => 'show', :id => @group
    else
      render :action => 'edit'
    end
  end

  def destroy
    Group.find(params[:id]).destroy
    redirect_to :action => 'list_user'
  end
  

  
end
